package com.chengfei.spring.cloud.weather.job;

import com.chengfei.spring.cloud.weather.service.CityDataService;
import com.chengfei.spring.cloud.weather.service.WeatherDataService;
import com.chengfei.spring.cloud.weather.vo.City;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;

/**
 * @author chengfei cheng.fei@chinaott.onaliyun.com
 **/
public class WeatherDataSyncJob extends QuartzJobBean{
    private final static Logger logger = LoggerFactory.getLogger(WeatherDataSyncJob.class);
    @Autowired
    private CityDataService cityDataService;
    @Autowired
    private WeatherDataService weatherDataService;
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        logger.info("Weather Data Sync Job Start");
        //1.获取城市id列表

        try{
            List<City> cityList = cityDataService.listCity();
            //2.遍历城市id获取天气
            cityList.stream().forEach(city -> {
                String cityId = city.getCityId();
                logger.info("Weather Data Sync Job,cityId:"+cityId);
                weatherDataService.syncDataByCityId(cityId);
            });
            logger.info("Weather Data Sync Job End");
        }catch (Exception e){
            logger.error("Exception!",e);
        }

    }
}
